<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="72px">
      <el-form-item label="类型名称" prop="employeeTypeName">
        <el-input
          v-model="queryParams.employeeTypeName"
          placeholder="请输入从业人员类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="visible">
        <el-select v-model="queryParams.visible" placeholder="请选择从业人员类型状态" clearable size="small">
          <el-option
            v-for="dict in visibleOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:coursetype:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="coursetypeList"
      row-key="employeeTypeId"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="employeeTypeName" label="从业人员类型名称" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="orderNum" label="排序"></el-table-column>
      <el-table-column prop="visible" label="可见" :formatter="visibleFormat"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:menu:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:menu:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:menu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改课程类型管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="96px">
          <el-form-item label="上级类型" prop="parentId">
            <treeselect
              v-model="form.parentId"
              :options="courseOptions"
              :show-count="true"
              placeholder="选择上级从业人员类型"
            />
          </el-form-item>
        <el-form-item label="人员类型种类">
          <el-radio-group v-model="form.employeeTypeCategory" prop="employeeTypeName">
            <el-radio
              v-for="dict in employeeTypeNameOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
          <el-form-item label="类型名称" prop="employeeTypeName">
            <el-input v-model="form.employeeTypeName" placeholder="请输入类型名称" />
          </el-form-item>
          <el-form-item label="显示排序" prop="orderNum">
            <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
          </el-form-item>

        <el-form-item label="状态">
          <el-radio-group v-model="form.visible">
            <el-radio
              v-for="dict in visibleOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="课程类型图标" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入课程类型图标" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEmployeetype,
  getEmployeetype,
  delEmployeetype,
  addEmployeetype,
  treeselect,
  updateEmployeetype,
  exportEmployeetype
} from "@/api/wechat/employeetype";



import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";

export default {
  components: { Treeselect, IconSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 菜单树选项
      courseOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 课程类型管理表格数据
      coursetypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 课程类型的类型字典
      employeeTypeNameOptions: [],
      // 课程类型状态字典
      visibleOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeTypeName: undefined,
        parentId: undefined,
        orderNum: undefined,
        employeeTypeName: undefined,
        visible: undefined,
        icon: undefined
      },
      // 表单参数
      form: {
        employeeTypeId: undefined,
        employeeTypeName: undefined,
        parentId: undefined,
        orderNum: undefined,
        employeeTypeCategory: "0",
        visible: "0",
        icon: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      },
      // 表单校验
      rules: {
        employeeTypeName: [
          { required: true, message: "人员类型名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("employee_type_category").then(response => {
      this.employeeTypeNameOptions = response.data;
    });
    this.getDicts("sys_show_hide").then(response => {
      this.visibleOptions = response.data;
    });
  },
  methods: {
    /** 查询课程类型管理列表 */
    getList() {
      this.loading = true;
      listEmployeetype(this.queryParams).then(response => {
        this.coursetypeList = response.data;
        this.loading = false;
      });
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.courseOptions = [];
        const courseType = { id: 0, label: "主类目", children: [] };
        courseType.children = response.data;
        this.courseOptions.push(courseType);
      });
    },
    // 课程类型的类型字典翻译
    employeeTypeNameFormat(row, column) {
      return this.selectDictLabel(
        this.employeeTypeNameOptions,
        row.employeeTypeName
      );
    },
    // 课程类型状态字典翻译
    visibleFormat(row, column) {
      return this.selectDictLabel(this.visibleOptions, row.visible);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        employeeTypeId: undefined,
        employeeTypeName: "",
        parentId: undefined,
        orderNum: 1,
        employeeTypeCategory: "0",
        visible: "0",
        icon: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.employeeTypeId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
       if (row != null) {
        this.form.parentId = row.employeeTypeId;
      }
      this.open = true;
      this.title = "添加从业人员类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      const employeeTypeId = row.employeeTypeId || this.ids;
      getEmployeetype(employeeTypeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改从业人员类型";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.employeeTypeId != undefined) {
            updateEmployeetype(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addEmployeetype(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const employeeTypeIds = row.employeeTypeId || this.ids;
      this.$confirm(
        '是否确认删除课程类型管理编号为"' + employeeTypeIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delEmployeetype(employeeTypeIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    }
  }
};
</script>